package xnj.lazydog.btcontroller.MyViews;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class IDBitmap {

    public static final int BMP_MODE_STRICT =0;
    public static final int BMP_MODE_WIDTH_ONLY =1;
    public static final int BMP_MODE_HEIGHT_ONLY =2;
    public static final int BMP_MODE_WRAP =3;

    public static Bitmap getBitmapScaled(Resources res,int id, int width, int height, int mode)
    {
        Bitmap b= BitmapFactory.decodeResource(res,id);
        int bw,bh;
        bw = b.getWidth();
        bh = b.getHeight();
        switch (mode)
        {
            case BMP_MODE_STRICT:
                return  Bitmap.createScaledBitmap(b,width,height,true);
            case BMP_MODE_WIDTH_ONLY:
                return Bitmap.createScaledBitmap(b,width,bh*width/bw,true);
            case BMP_MODE_HEIGHT_ONLY:
                return Bitmap.createScaledBitmap(b,bw*height/bh,height,true);
            default:
                if(width*bh<height*bw)
                    return Bitmap.createScaledBitmap(b,width,bh*width/bw,true);
                else
                    return Bitmap.createScaledBitmap(b,bw*height/bh,height,true);
        }
    }

    public static Bitmap getBitmapScaled(Resources res,int id, float fwidth, float fheight, int mode)
    {
        Bitmap b= BitmapFactory.decodeResource(res,id);
        int bw,bh;
        bw = b.getWidth();
        bh = b.getHeight();
        int width = (int)fwidth,height = (int)fheight;

        switch (mode)
        {
            case BMP_MODE_STRICT:
                return  Bitmap.createScaledBitmap(b,width,height,true);
            case BMP_MODE_WIDTH_ONLY:
                return Bitmap.createScaledBitmap(b,width,bh*width/bw,true);
            case BMP_MODE_HEIGHT_ONLY:
                return Bitmap.createScaledBitmap(b,bw*height/bh,height,true);
            default:
                if(width*bh<height*bw)
                    return Bitmap.createScaledBitmap(b,width,bh*width/bw,true);
                else
                    return Bitmap.createScaledBitmap(b,bw*height/bh,height,true);
        }
    }



    public static  File getNewFile(String dir,String name)
    {
        File file = Environment.getExternalStorageDirectory();
        String root = file.getAbsolutePath();
        // 判断是否有外部存储设备
        if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
            return null;
        if(!file.exists())
            return null;
        // 确保要存放图片的文件夹存在
        File dir_file = new File(root+"/"+dir+"/");
        if(!dir_file.exists()) {
            if(!dir_file.mkdir())
                return null;
        }
        File img = new File(root+"/"+dir+"/"+name);
        if(!img.exists())
        {
            try {
                img.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }
        return img;
    }

    private static final String TAG = "IDBitmap";
    public static boolean saveBitmap(Bitmap bitmap,String dir_name,String name)
    {
        File file = Environment.getExternalStorageDirectory();
        String root = file.getAbsolutePath();
        // 判断是否有外部存储设备
        if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
            return false;
        if(!file.exists())
            return false;
        // 确保要存放图片的文件夹存在
        File dir_file = new File(root+"/"+dir_name+"/");
        if(!dir_file.exists()) {
            if(!dir_file.mkdir())
                return false;
        }

        // 创建图片文件
        File img = new File(root+"/"+dir_name+"/"+name);
        if(!img.exists())
        {
            try {
                img.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
        }

        try {
            BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(img));
            bitmap.compress(Bitmap.CompressFormat.PNG,100,outputStream);
            outputStream.flush();
            outputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        //       Log.w(TAG, "saveBitmap: "+file.getAbsolutePath()+"/"+dir_name+"/"+name);

        return true;
    }

}
